<template>
  <div style="padding: 10px;width: 100%">
    <!--  功能区   -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
    </div>
    <!--  搜索区   -->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable/>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
    </div>

    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column prop="id" label="ID" sortable />
      <el-table-column prop="title" label="标题"  />
      <el-table-column prop="author" label="作者"  />
      <el-table-column prop="time" label="时间"  />
      <el-table-column fixed="right" label="Operations" >
        <template #default="scope">
          <el-button  size="small"  @click="details(scope.row)">详情</el-button>
          <el-button  size="small"  @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small"  type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          :current-page="currentPage4"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog v-model="dialogVisible" title="新增用户" width="50%">
      <el-form  :model="form" label-width="50%">
        <el-form-item label="标题" style="width: 80%;">
          <el-input v-model="form.title" />
        </el-form-item>
          <div id="div1"></div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save()">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog  title="详情" v-model="vis"  width="50%">
      <el-card>
        <div v-html="detail.content" style="min-height: 100px"></div>
      </el-card>
    </el-dialog>

  </div>
</template>

<script>


import request from "@/utils/request";
import E from 'wangeditor'

let editor

export default {
  name: 'NewsView',
  components: {
  },
  data(){
    return{
      form:{},
      dialogVisible:false,
      total:11,
      currentPage4:1,
      pageSize:10,
      search:"",
      tableData: [],
      vis:false,
      detail:{}
    }
  },
  created(){
    this.load();
  },
  methods:{
    details(row){
      this.detail = row
      this.vis = true
    },
    load(){
      request.get("/news",{
        params:{
          pageNum:this.currentPage4,
          pageSize:this.pageSize,
          search:this.search
        }
      }).then(res =>{
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total;
      })
    },
    add(){
      this.dialogVisible=true;
      this.form={};//清空一下表单域

      if(this.$refs['upload']){
        this.$refs['upload'].clearFiles();//清除历史文件列表
      }
      //关联弹窗里面的div，做一个富文本编辑器
      this.$nextTick(()=>{
        editor = new E('#div1')

        //配置server接口地址
        editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
        editor.config.uploadFileName = "file" //设置上传文件名称
        editor.create()
      })
    },
    save(){
      this.form.content=editor.txt.html() //获取编辑器里面的值，然后赋予到实体当中

      if(this.form.id){ //更新
        request.put("/news",this.form).then(res =>{
          console.log(res)
          if(res.code==0){
            this.$message.success("更新成功");
            this.dialogVisible=false;
          }else{
            this.$message.error(res.msg);
          }
        })
      }else{ //新增
        let userStr= sessionStorage.getItem("user") || "{}"
        let user = JSON.parse(userStr)
        console.log("@@@",user)
        this.form.author=user.username

        request.post("/news",this.form).then(res =>{
          console.log(res)
          if(res.code==0){
            this.$message.success("新增成功");
            this.dialogVisible=false;
          }else{
            this.$message.error(res.msg);
          }
        })
      }
      this.load();
    },
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row)) //深度拷贝
      this.dialogVisible=true;

      if(this.$refs['upload']){
        this.$refs['upload'].clearFiles();//清除历史文件列表
      }
      //关联弹窗里面的div，做一个富文本编辑器
      this.$nextTick(()=>{
        if(!editor){
          editor = new E('#div1')
          editor.create()
        }
        editor.txt.html(this.form.content)
      })
    },
    handleDelete(id){
      console.log(id)
      request.delete("/news/"+id).then(res =>{
        console.log(res)
        if(res.code==0){
          this.$message.success("删除成功");
          this.load();
        }else{
          this.$message.error(res.msg);
        }
      })
    },
    handleSizeChange(pageSize){ //改变当前每页的个数
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum){//改变当前页码
      this.currentPage4=pageNum;
      this.load();
    },
    handleAvatarSuccess(res){
      console.log("打印信息：",res);
      this.form.cover=res.data;
    }
  }
}
</script>
